package org.locker.service.impl;

import org.apache.commons.codec.digest.DigestUtils;
import org.locker.common.core.utils.MapstructUtils;
import org.locker.common.core.utils.StringUtils;
import org.locker.common.mybatis.core.page.TableDataInfo;
import org.locker.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.locker.common.oss.core.OssClient;
import org.locker.common.oss.factory.OssFactory;
import org.locker.system.domain.vo.SysOssVo;
import org.locker.system.service.ISysOssService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.locker.domain.bo.BluetoothVersionBo;
import org.locker.domain.vo.BluetoothVersionVo;
import org.locker.domain.BluetoothVersion;
import org.locker.mapper.BluetoothVersionMapper;
import org.locker.service.IBluetoothVersionService;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 蓝牙升级信息Service业务层处理
 *
 * @author winnie
 * @date 2023-10-11
 */
@RequiredArgsConstructor
@Service
public class BluetoothVersionServiceImpl  extends ServiceImpl<BluetoothVersionMapper,BluetoothVersion>  implements IBluetoothVersionService {

    private final BluetoothVersionMapper baseMapper;
    private final ISysOssService sysOssService;

    /**
     * 查询蓝牙升级信息
     */
    @Override
    public BluetoothVersionVo queryById(Long bluetoothVersionId){
        return baseMapper.selectVoById(bluetoothVersionId);
    }

    /**
     * 查询蓝牙升级信息列表
     */
    @Override
    public TableDataInfo<BluetoothVersionVo> queryPageList(BluetoothVersionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BluetoothVersion> lqw = buildQueryWrapper(bo);
        Page<BluetoothVersionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询蓝牙升级信息列表
     */
    @Override
    public List<BluetoothVersionVo> queryList(BluetoothVersionBo bo) {
        LambdaQueryWrapper<BluetoothVersion> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BluetoothVersion> buildQueryWrapper(BluetoothVersionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BluetoothVersion> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPositionId() != null, BluetoothVersion::getPositionId, bo.getPositionId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceNo()), BluetoothVersion::getDeviceNo, bo.getDeviceNo());
        lqw.like(StringUtils.isNotBlank(bo.getVersionName()), BluetoothVersion::getVersionName, bo.getVersionName());
        lqw.eq(StringUtils.isNotBlank(bo.getVersionCode()), BluetoothVersion::getVersionCode, bo.getVersionCode());
        lqw.eq(StringUtils.isNotBlank(bo.getFileUrl()), BluetoothVersion::getFileUrl, bo.getFileUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getFileMd5()), BluetoothVersion::getFileMd5, bo.getFileMd5());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BluetoothVersion::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getMiniVersionCode()), BluetoothVersion::getMiniVersionCode, bo.getMiniVersionCode());
        lqw.eq(StringUtils.isNotBlank(bo.getFileChannel()), BluetoothVersion::getFileChannel, bo.getFileChannel());
        return lqw;
    }

    /**
     * 新增蓝牙升级信息
     */
    @Override
    public Boolean insertByBo(BluetoothVersionBo bo) {
        BluetoothVersion add = MapstructUtils.convert(bo, BluetoothVersion.class);


        if (StringUtils.isNotBlank(bo.getFileUrl())){
            SysOssVo sysOss = sysOssService.getById(Long.valueOf(bo.getFileUrl()));
            OssClient storage = OssFactory.instance(sysOss.getService());
            try {
                String md5Hex = DigestUtils.md5Hex(storage.getObjectContent(sysOss.getUrl()));
                add.setFileMd5(md5Hex);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBluetoothVersionId(add.getBluetoothVersionId());
        }
        return flag;
    }

    /**
     * 修改蓝牙升级信息
     */
    @Override
    public Boolean updateByBo(BluetoothVersionBo bo) {
        BluetoothVersion update = MapstructUtils.convert(bo, BluetoothVersion.class);

        if (StringUtils.isNotBlank(bo.getFileUrl())){
            SysOssVo sysOss = sysOssService.getById(Long.valueOf(bo.getFileUrl()));
            OssClient storage = OssFactory.instance(sysOss.getService());
            try {
                String md5Hex = DigestUtils.md5Hex(storage.getObjectContent(sysOss.getUrl()));
                update.setFileMd5(md5Hex);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BluetoothVersion entity){
        //做一些数据校验,如唯一约束
    }

    /**
     * 批量删除蓝牙升级信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
